#include "toast.h"

#include <QScreen>
#include <QApplication>
#include <QTimer>
#include <QVBoxLayout>
#include <QLabel>


Toast::Toast(const QString& text)
{
    //设置基本属性
    this->setFixedSize(800,150);
    this->setWindowTitle("消息通知");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->setStyleSheet("QDialog { background-color: rgb(255,255,255); }");
    //去掉标题的标题栏
    this->setWindowFlags(Qt::FramelessWindowHint);

    //获取窗口位置
    //priprimaryScreen获取整个屏幕的尺寸
    QScreen* screen = QApplication::primaryScreen();
    int width = screen->size().width();
    int height = screen->size().height();
    int x = (width -  this->width()) /2;
    int y = height - this->height() - 100;
    this->move(x,y);

    //创建布局管理器
    QVBoxLayout* layout = new QVBoxLayout();
    layout->setSpacing(0);
    layout->setContentsMargins(0,0,0,0);
    this->setLayout(layout);

    //显示文本内容
    QLabel* label = new QLabel();
    label->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    label->setAlignment(Qt::AlignCenter);
    label->setStyleSheet("QLabel { font-size: 32px; }");
    label->setText(text);
    layout->addWidget(label);

    //设置定时器，2s后关闭
    QTimer* timer = new QTimer(this);
    connect(timer,&QTimer::timeout,this,[=](){
        //关闭定时器
        timer->stop();
        //将窗口关闭掉
        this->close();
    });

    //2s后，关闭定时器
    timer->start(2000);
}


void Toast::showMessage(const QString& text)
{
    Toast* toast = new Toast(text);
    toast->show();
}
